package com.yugao.fintech.antelope.base.model.enums;

import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 配置定义枚举, 主要应多多配置场景, 比如一个存储配置会有多个渠道, minio, oss, 七牛云等等, 他们都属于存储配置
 * , 又比如短信渠道, 阿里云短信, 七牛云短信, 华为元短信等等, 他们都属于短信配置
 *

 */
@Getter
@AllArgsConstructor
public enum ConfigDefineEnum {
    DEFAULT(ofKeys(ConfigKeyEnum.DEFAULT)),
    STORAGE(ofKeys(ConfigKeyEnum.STORAGE_MINIO, ConfigKeyEnum.STORAGE_ALIYUN)),
    SMS(ofKeys(ConfigKeyEnum.SMS_ALIYUN, ConfigKeyEnum.SMS_JUMEI, ConfigKeyEnum.SMS_TENCENT)),
    EMAIL(ofKeys(ConfigKeyEnum.EMAIL_163, ConfigKeyEnum.EMAIL_TENCENT)),

    WX_MA(ofKeys(ConfigKeyEnum.WX_MA)),
    WX_MP(ofKeys(ConfigKeyEnum.WX_MP)),
    // 企业微信
    WX_CP(ofKeys(ConfigKeyEnum.WX_CP)),
    DINGTALK(ofKeys(ConfigKeyEnum.DINGTALK)),
    GITEE(ofKeys(ConfigKeyEnum.GITEE)),

    WEB_SITE(ofKeys(ConfigKeyEnum.WEB_SITE)),
    PUSH_PC(ofKeys(ConfigKeyEnum.PUSH_SYSTEM_PC)),
    PUSH_APP(ofKeys(ConfigKeyEnum.PUSH_APP)),
    COPYRIGHT(ofKeys(ConfigKeyEnum.COPYRIGHT)),
    USER_REGISTER(ofKeys(ConfigKeyEnum.USER_REGISTER)),
    ;

    private final List<ConfigKeyEnum> keys;

    private final static Map<String, ConfigDefineEnum> map = new HashMap<>();

    static {
        for (ConfigDefineEnum value : ConfigDefineEnum.values()) {
            value.keys.stream().map(ConfigKeyEnum::getKey).forEach(e -> map.put(e, value));
        }
    }

    static List<ConfigKeyEnum> ofKeys(ConfigKeyEnum... keys) {
        return Arrays.stream(keys).collect(Collectors.toList());
    }

    /**
     * 如果能找到就采用 code, 否则返回 configKey
     */
    public static ConfigDefineEnum ofByKey(String configKey) {
        ConfigDefineEnum configDefineEnum = map.get(configKey);
        if (Objects.isNull(configDefineEnum)) {
            throw new BizException("尚未包含配置定义, configKey: {}", configKey);
        }
        return configDefineEnum;
    }

    public static ConfigDefineEnum ofByKey(ConfigKeyEnum configKey) {
        if (Objects.isNull(configKey)) {
            throw new BizException("configKey is null");
        }
        return ofByKey(configKey.getKey());
    }
}
